<!--
 * @Descripttion: 视频管理
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-18 13:55:40
-->
<template>
  <div class="lb-sys-sproconfig">
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          size="medium"
          type="primary"
          @click="$router.push(appPath + '/shortvideo/edit')"
          icon="el-icon-plus"
          >{{ $t('menu.ShortVideoAdd') }}</lb-button
        >
      </el-row>
      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="视频名称" prop="title">
            <el-input
              v-model="searchForm.title"
              placeholder="请输入视频名称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" width="80" label="ID"> </el-table-column>
        <el-table-column
          prop="title"
          width="150"
          show-overflow-tooltip
          label="视频名称"
        >
        </el-table-column>
        <el-table-column prop="cover" label="视频封面">
          <template slot-scope="scope">
            <lb-image :src="scope.row.cover" />
          </template>
        </el-table-column>
        <el-table-column prop="tag" label="标签"> </el-table-column>
        <el-table-column prop="password" label="动态密码"> </el-table-column>
        <el-table-column prop="browse_count" label="浏览量"> </el-table-column>
        <el-table-column prop="send_count" label="转发量"> </el-table-column>
        <el-table-column prop="top" label="排序值"> </el-table-column>
        <el-table-column prop="status" label="是否展示">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="toConfirmEnd(scope.row.id, scope.row.status)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  $router.push(appPath + '/shortvideo/edit?id=' + scope.row.id)
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="toEnd(scope.row.id, -1)"
                >{{ $t('action.delete') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="success"
                @click="
                  $router.push(
                    appPath + '/shortvideo/record?id=' + scope.row.id
                  )
                "
                >{{ $t('action.viewData') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      appPath: '',
      searchForm: {
        title: '',
        limit: 10,
        page: 1
      },
      loading: false,
      total: 0,
      tableData: [],
      downloadDialog: false,
      staffArr: [],
      downloadForm: {
        id: '',
        staff_id: '',
        page: 'reduction/pages/detail'
      },
      reductionSrc: ''
    }
  },
  created () {
  },
  activated () {
    let { path } = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) {
        this.searchForm.page = 1
        this.tableData = []
      }
      this.loading = true
      let { searchForm } = this
      let { code, data } = await this.$api.shortvideoList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      console.log(this.tableData)
      for(let itemData of this.tableData){
        itemData.password = !!itemData.password ?itemData.password :"";
      }
      this.total = data.total
    },
    async toEnd (id, status) {
      let confirmText = this.$t('tips.confirmDelete')
      this.$confirm(confirmText, this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.toConfirmEnd(id, status)
      }).catch(() => {

      })
    },
    async toConfirmEnd (id, status) {
      let { code } = await this.$api.shortvideoStatus({ id, status })
      if (code === 200) {
        this.$message.success(
          this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper')
        )
        if (status === -1) {
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        }
      } else {
        if (status === -1) return
        this.getTableDataList()
      }
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input,
.el-select {
  width: 200px;
}
.dialog-inner {
  height: 300px;
  .el-image {
    width: 200px;
    height: 200px;
    margin: 0 auto;
  }
}
</style>
